package Fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONObject;

import www.truongnha.com.R;
import Config.setting;
import TruongNhaLib.JSONParser;
import TruongNhaLib.Lesson;
import TruongNhaLib.Schedule;
import TruongNhaLib.UserFunctions;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

public class TimeTableFragment extends Fragment {
    private Schedule  schedule = new Schedule();
    GridView grid2,grid3,grid4,grid5,grid6,grid7;
    ListAdapter MonAdapter, WenAdapter, TueAdapter, ThiAdapter, FriAdapter, SatAdapter ;
    ArrayList<HashMap<String, String>> timeMon = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> timeWen = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> timeTue = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> timeThi = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> timeFri = new ArrayList<HashMap<String,String>>();
    ArrayList<HashMap<String, String>> timeSat = new ArrayList<HashMap<String,String>>();
	//private ArrayAdapter<Lesson> adapter;
    private Intent doneLoading;
    private static String host = setting.getHost();
    private static String url_schedule = host + "api/schedule/";
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
	  View view = inflater.inflate(R.layout.timetable, container, false);
	  new getTimeTask().execute(url_schedule);
    // Get data for main Class
	MonAdapter = new SimpleAdapter(getActivity().getApplicationContext(), timeMon,
                R.layout.lesson_item,
                new String[] { "time", "className", "subject"}, new int[] {
                        R.id.time ,R.id.className, R.id.subject});
	WenAdapter = new SimpleAdapter(getActivity().getApplicationContext(), timeWen,
            R.layout.lesson_item,
            new String[] { "time", "className", "subject"}, new int[] {
                    R.id.time ,R.id.className, R.id.subject});
	TueAdapter = new SimpleAdapter(getActivity().getApplicationContext(), timeTue,
            R.layout.lesson_item,
            new String[] { "time", "className", "subject"}, new int[] {
                    R.id.time ,R.id.className, R.id.subject});
	ThiAdapter = new SimpleAdapter(getActivity().getApplicationContext(), timeThi,
            R.layout.lesson_item,
            new String[] { "time", "className", "subject"}, new int[] {
                    R.id.time ,R.id.className, R.id.subject});
	FriAdapter = new SimpleAdapter(getActivity().getApplicationContext(), timeFri,
            R.layout.lesson_item,
            new String[] { "time", "className", "subject"}, new int[] {
                    R.id.time ,R.id.className, R.id.subject});
	SatAdapter = new SimpleAdapter(getActivity().getApplicationContext(), timeSat,
            R.layout.lesson_item,
            new String[] { "time", "className", "subject"}, new int[] {
                    R.id.time ,R.id.className, R.id.subject});
	Log.d("time", "3");
	//Set adapter  
	grid2 = (GridView)view.findViewById(R.id.grid2);
	grid3 = (GridView)view.findViewById(R.id.grid3);
	grid4 = (GridView)view.findViewById(R.id.grid4);
	grid5 = (GridView)view.findViewById(R.id.grid5);
	grid6 = (GridView)view.findViewById(R.id.grid6);
	grid7 = (GridView)view.findViewById(R.id.grid7);
	grid2.setAdapter(MonAdapter);
	grid3.setAdapter(TueAdapter);
	grid4.setAdapter(WenAdapter);
	grid5.setAdapter(ThiAdapter);
	grid6.setAdapter(FriAdapter);
	grid7.setAdapter(SatAdapter);
	  return view;
  }


	class getTimeTask extends AsyncTask<String, Void, Void>{
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			doneLoading = new Intent();
            doneLoading.setAction("DONE_LOADING_ACTION");
            doneLoading.putExtra("doneLoading", false);
            getActivity().getApplicationContext().sendBroadcast(doneLoading);
			Log.d("Time", "start Load");
		}
		@Override
		protected Void doInBackground(String... params) {
			// TODO Auto-generated method stub
	        JSONObject timeObject = new JSONParser().getJSONObject(params[0]);
			schedule = new Schedule(timeObject, UserFunctions.userType( getActivity().getApplicationContext() ));
	        return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			//Broadcast to main activity when done loading
			doneLoading = new Intent();
            doneLoading.setAction("DONE_LOADING_ACTION");
            doneLoading.putExtra("doneLoading", true);
            getActivity().getApplicationContext().sendBroadcast(doneLoading);            

		    List<Lesson> MonList = schedule.getMon();
	    	//add day(Mon, Tue, ) title first
		    HashMap<String,String> daytitle = new HashMap<String,String>();
		    daytitle.put("time", "");
		    daytitle.put("className", "");
		    daytitle.put("subject", "Thứ 2");
		    timeMon.add(daytitle);
		    for(int i = 0 ; i< MonList.size() ; i++ ){
		    	HashMap<String,String> lesson = new HashMap<String,String>();
			    lesson.put("time", MonList.get(i).getTime()+"");
			    lesson.put("className", MonList.get(i).getClsName());
			    lesson.put("subject", MonList.get(i).getSub());
			    timeMon.add(lesson);
		    }
		    ((BaseAdapter) MonAdapter).notifyDataSetChanged();
		    MonList = schedule.getTue();
		    daytitle = new HashMap<String,String>();
		    daytitle.put("time", "");
		    daytitle.put("className", "");
		    daytitle.put("subject", "Thứ 3");
		    timeTue.add(daytitle);
		    for(int i = 0 ; i< MonList.size() ; i++ ){
		    	HashMap<String,String> lesson = new HashMap<String,String>();
			    lesson.put("time", MonList.get(i).getTime()+"");
			    lesson.put("className", MonList.get(i).getClsName());
			    lesson.put("subject", MonList.get(i).getSub());
			    timeTue.add(lesson);
		    }
		    ((BaseAdapter) TueAdapter).notifyDataSetChanged();

		    MonList = schedule.getWen();
		    daytitle = new HashMap<String,String>();
		    daytitle.put("time", "");
		    daytitle.put("className", "");
		    daytitle.put("subject", "Thứ 4");
		    timeWen.add(daytitle);
		    for(int i = 0 ; i< MonList.size() ; i++ ){
		    	HashMap<String,String> lesson = new HashMap<String,String>();
			    lesson.put("time", MonList.get(i).getTime()+"");
			    lesson.put("className", MonList.get(i).getClsName());
			    lesson.put("subject", MonList.get(i).getSub());
			    timeWen.add(lesson);
		    }
		    ((BaseAdapter) WenAdapter).notifyDataSetChanged();

		    MonList = schedule.getThi();
		    daytitle = new HashMap<String,String>();
		    daytitle.put("time", "");
		    daytitle.put("className", "");
		    daytitle.put("subject", "Thứ 5");
		    timeThi.add(daytitle);
		    for(int i = 0 ; i< MonList.size() ; i++ ){
		    	HashMap<String,String> lesson = new HashMap<String,String>();
			    lesson.put("time", MonList.get(i).getTime()+"");
			    lesson.put("className", MonList.get(i).getClsName());
			    lesson.put("subject", MonList.get(i).getSub());
			    timeThi.add(lesson);
		    }
		    ((BaseAdapter) ThiAdapter).notifyDataSetChanged();
		    
		    MonList = schedule.getFri();
		    daytitle = new HashMap<String,String>();
		    daytitle.put("time", "");
		    daytitle.put("className", "");
		    daytitle.put("subject", "Thứ 6");
		    timeFri.add(daytitle);
		    for(int i = 0 ; i< MonList.size() ; i++ ){
		    	HashMap<String,String> lesson = new HashMap<String,String>();
			    lesson.put("time", MonList.get(i).getTime()+"");
			    lesson.put("className", MonList.get(i).getClsName());
			    lesson.put("subject", MonList.get(i).getSub());
			    timeFri.add(lesson);
		    }
		    ((BaseAdapter) FriAdapter).notifyDataSetChanged();
		    
		    MonList = schedule.getSat();
		    daytitle = new HashMap<String,String>();
		    daytitle.put("time", "");
		    daytitle.put("className", "");
		    daytitle.put("subject", "Thứ 7");
		    timeSat.add(daytitle);
		    for(int i = 0 ; i< MonList.size() ; i++ ){
		    	HashMap<String,String> lesson = new HashMap<String,String>();
			    lesson.put("time", MonList.get(i).getTime()+"");
			    lesson.put("className", MonList.get(i).getClsName());
			    lesson.put("subject", MonList.get(i).getSub());
			    timeSat.add(lesson);
		    }
		    ((BaseAdapter) SatAdapter).notifyDataSetChanged();
		    
			Log.d("Time", "Done");
		}
	}//End class getTimeTask


} 
